#include "syscall.h"

void initTests(){
	int lock1 = -1;
	int lock2 = -1;
	int cond1 = -1;
	int MV1 = -1;
	
	Printing("T2: initializing everything!\n", ConsoleOutput, 0, 0);
	
	lock1 = CreateLock("lock1", 5);
	/*Printing("lock1 index = %i\n", ConsoleOutput, lock1, 0);*/
	Printing("T2: lock1 created!\n", ConsoleOutput, 0, 0);
	if(lock1 == -1){
		Printing("T2: Error when trying to create lock 1!\n", ConsoleOutput, 0, 0);
	}
	
	lock2 = CreateLock("lock2", 5);
	/*Printing("lock2 index = %i\n", ConsoleOutput, lock2, 0);*/
	Printing("T2: lock2 created!\n", ConsoleOutput, 0, 0);
	if(lock2 == -1){
		Printing("T2: Error when trying to create lock 2!\n", ConsoleOutput, 0, 0);
	}

	cond1 = CreateCondition("condition1", 10);
	/*Printing("cond1 index = %i\n", ConsoleOutput, cond1, 0);*/
	Printing("T2: condition1 created!\n", ConsoleOutput, 0, 0);
	if(cond1 == -1){
		Printing("T2: Error when trying to create cond 1!\n", ConsoleOutput, 0, 0);
	}

	MV1 = CreateMonitor("monitor1", 8);
	/*Printing("monitor1 index = %i\n", ConsoleOutput, MV1, 0);*/
	Printing("T2: monitor1 created!\n", ConsoleOutput, 0, 0);
	if(MV1 == -1){
		Printing("T2: Error when trying to create MV 1!\n", ConsoleOutput, 0, 0);
	}
	
}

void testSignalWithMonitor(){
	Printing("T2: Attempting to acquire lock 1\n", ConsoleOutput, 0, 0);
	AcquireLock("lock1", 5);
	Printing("T2: Acquired lock 1, about to set monitor value to 1\n", ConsoleOutput, 0, 0);
	SetMonitor("monitor1", 8, 1, 0);
	Printing("T2: Monitor value should be 1 now\n", ConsoleOutput, 0, 0);
	Signal("condition1", 10, "lock1", 5);
	Printing("T2: Signaled T1, about to release lock 1\n", ConsoleOutput, 0, 0);
	ReleaseLock("lock1", 5);
	Printing("T2: Released lock 1, about to exit\n", ConsoleOutput, 0, 0);
}

int main(){
	initTests();
	testSignalWithMonitor();
	Exit(12);
	return 1;
}